import { reqGetChannels, reqGetAllChannels } from '@/api/channels'

const state = {
  channels: [],
  allChannels: []
}

const mutations = {
  setChannels(state, channels) {
    state.channels = channels
  },

  setAllChannels(state, allChannels) {
    state.allChannels = allChannels
  },

  addChannels(state, channel) {
    state.channels.push(channel)
  },

  delChannels(state, index) {
    state.channels.splice(index, 1)
  }
}

const actions = {
  async getChannelsAsync(context) {
    const { data: { channels }} = await reqGetChannels()
    context.commit('setChannels', channels)
  },

  async getAllChannelsAsync(context) {
    const { data: { channels }} = await reqGetAllChannels()
    context.commit('setAllChannels', channels)
  }
}

const getters = {
  optionChannels(state) {
    return state.allChannels.filter(channel => {
      const index = state.channels.findIndex(item => item.id === channel.id)
      return index === -1
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
